package com.mall.manager.user.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.util.WebUtils;

import com.mall.untils.MD5Util;
import com.mall.untils.MapUtil;
import com.mall.untils.Page;

/**
 * 
 * @author LISIQIANG
 * 处理用户增、删、改、查业务层
 */
@Service("userService")
public class ManageUserService {
	
	private Logger logger = LoggerFactory.getLogger(ManageUserService.class);
    @Resource
    private JdbcTemplate jdbcTemplate;
    
    
	
    /**
     * 添加用户
     * @param paraMap
     * @return
     */
	public int manager_user_add(Map<String, Object> paraMap){
		
		String user_name = MapUtils.getString(paraMap, "user_name");
		String pwd = MD5Util.convertMD5(MapUtils.getString(paraMap, "pwd"));
		String real_name = MapUtils.getString(paraMap, "real_name");
		int age = MapUtils.getIntValue(paraMap, "age");	
		int sex = MapUtils.getIntValue(paraMap, "sex");	
		String address = MapUtils.getString(paraMap, "address");
		String telephone = MapUtils.getString(paraMap, "telephone");
		String email = MapUtils.getString(paraMap, "email");			
		String qq = MapUtils.getString(paraMap, "qq");
		
		try {
			String sqlCheck="select * from t_user where  ";
			sqlCheck += " user_name = "+"'"+ user_name.trim()+ "'";
			
			List<Map<String, Object>> map = jdbcTemplate.queryForList(sqlCheck);			
			if(map!=null&&map.size()>0){
				return  -1;			
				}
			
			String sql="insert into t_user(user_name,pwd,real_name,age,sex,address,telephone,email,qq,create_time,update_time) values(?,?,?,?,?,?,?,?,?,now(),now())";
			int update = this.jdbcTemplate.update(sql,user_name,pwd,real_name,age,sex,address,telephone,email,qq);
			return update;
			
			} catch (Exception e) {
				logger.info("添加用户失败！");
				return 0;
			}		
		
	}
	
	
	
	
	/**按照Id查找用户相关信息
	 * @param paraMap
	 * @return
	 */
	public Map<String, Object> manager_user_updateone(Map<String, Object> paraMap) {
		int id = MapUtils.getIntValue(paraMap, "id");
		try {
			String sql="select * from t_user where id=?";
			Map<String, Object> userMap = jdbcTemplate.queryForMap(sql,id);
			return userMap;
		} catch (Exception e) {
			logger.info("查找修改的用户失败！");
			return null;
		}
	}
	
	
	
	/**
	 * 用户修改
	 * @param paraMap
	 * @return
	 */
	
	public int manager_user_edit(Map<String, Object> paraMap) {
		int id = MapUtils.getIntValue(paraMap, "id");		
		String user_name = MapUtil.getStringValue(paraMap, "user_name");
		String pwd = MD5Util.convertMD5(MapUtil.getStringValue(paraMap, "pwd"));
		String real_name = MapUtil.getStringValue(paraMap, "real_name");
		String telephone = MapUtil.getStringValue(paraMap, "telephone");
		
		String address = MapUtil.getStringValue(paraMap, "address");      
		String email = MapUtil.getStringValue(paraMap, "email");
		String qq = MapUtil.getStringValue(paraMap, "qq");
		int age = MapUtils.getIntValue(paraMap, "age");
		int sex = MapUtils.getIntValue(paraMap, "sex"); ;
		
		String sql="update t_user set user_name=? , pwd=? , real_name=? ,telephone=?,address=?,email=?,qq=?,age=?,sex=?,update_time=now() where id=?";
		try {
			int update = jdbcTemplate.update(sql,user_name,pwd,real_name,telephone,address,email,qq,age,sex,id);
			
			return update;
		} catch (Exception e) {
			logger.info("注册信息有问题！");
			return 0;
		}
		
	}
	
	/**
	 * 用户删除
	 * @param paraMap
	 * @return
	 */
	@Transactional
	public int manager_user_delete(Map<String, Object> paraMap) {
		int id = MapUtils.getIntValue(paraMap, "id");
		try {
			String sqlPart = "delete from t_user_part where user_id=?";
			int updatePart = jdbcTemplate.update(sqlPart,id);
			if(updatePart==0){
				logger.info("删除与用户相关联的角色数据失败！"); 
			}
			
			String sql="delete from t_user where id=?";
			int update = jdbcTemplate.update(sql,id);
			if(update==0){
				logger.info("删除用户关联数据失败！");
			}
			
			return update;
		} catch (Exception e) {
			logger.info("删除用户失败！");
			return 0;
		}
	}

	/**
	 * 
	 * @param request
	 * @return
	 */
	public Page manager_user_list(HttpServletRequest request) {
		Page page = null; 
	    Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
	    int pageNo = MapUtils.getIntValue(paraMap, "currentpageno");
	    String user_name = MapUtils.getString(paraMap, "user_name");
	    int pageSize = 10;
    	try { 
 	        if (pageNo == 0){
 	        	pageNo = 1;
 	        }
 	        
 	       String sql = "select count(*) from t_user ";
 	       
 	       if(user_name != null && !user_name.equals("")){
 	    	  sql += " where user_name = '" + user_name +"'";
 	        } 	        
 	         	       
 	        @SuppressWarnings("deprecation")
			int total = this.jdbcTemplate.queryForInt(sql);
 	        int startIndex = Page.getStartOfPage(pageNo, pageSize); 	        
 	        sql = "select * from t_user " ;
 	        if(user_name != null && !user_name.equals("")){
  	    	  sql += " where user_name= '" + user_name +"'";
  	        }  	        
 	        sql +=" order by id desc limit " + startIndex + "," + pageSize;
 	        	        
 	        List<Map<String, Object>> queryForList = this.jdbcTemplate.queryForList(sql);
 	        page = new Page(startIndex, total, pageSize, queryForList);
 	        return page;
 	    } catch (Exception e) {
 	        this.logger.info("获取用户列表失败");
 	        return null;
 	    }
	}
	/**
	 * 用户列表list
	 * @param request
	 * @return
	 */
	public List manager_user_lists(HttpServletRequest request) {
	    Map paraMap = WebUtils.getParametersStartingWith(request, "");
	    String user_name = MapUtils.getString(paraMap, "user_name");
	    try{
 	        String sql = "";
 	        sql = "select * from t_user " ;
 	        if(user_name != null && !user_name.equals("")){
  	    	  sql += " where user_name= '" + user_name +"'";
  	        }  	        
 	        List queryForList = this.jdbcTemplate.queryForList(sql);
 	        return queryForList;
 	    } catch (Exception e) {
 	        this.logger.info("获取用户列表失败");
 	        return null;
 	    }
	}
}
